package com.egd.imapp.contoller.activity.contact;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.egd.imapp.R;
import com.egd.imapp.contoller.activity.chat.GroupChatActivity;
import com.egd.imapp.contoller.adapter.GroupListAdapter;
import com.egd.imapp.model.Model;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMGroup;
import com.hyphenate.easeui.constants.EaseConstant;
import com.hyphenate.easeui.widget.EaseTitleBar;
import com.hyphenate.exceptions.HyphenateException;

import java.util.List;

public class GroupListActivity extends AppCompatActivity {
    private static final String TAG = "TAG";
    private EaseTitleBar mTitleBar;
    private ListView mListView;
    private LinearLayout ll_create_new_group;
    private GroupListAdapter groupListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_list);

        initView();

        initData();

        initListener();

    }

    private void initData() {
        groupListAdapter = new GroupListAdapter(this);

        mListView.setAdapter(groupListAdapter);

        // 从环信服务器中获取所有群的信息
        getGroupsInfoFromServer();
    }

    private void getGroupsInfoFromServer() {
        Model.getInstance().getGlobalThreadPool().execute(new Runnable() {
            @Override
            public void run() {
                try {
                    List<EMGroup> groups = EMClient.getInstance().groupManager().getJoinedGroupsFromServer();

                    // 更新页面
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(GroupListActivity.this, "加载群信息成功", Toast.LENGTH_SHORT).show();

//                            groupListAdapter.refresh(groups);
                            refresh();

                        }
                    });
                } catch (HyphenateException e) {
                    e.printStackTrace();
                    // 更新页面
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(GroupListActivity.this, "加载群信息失败", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        });
    }


    private void refresh() {

        groupListAdapter.refresh(EMClient.getInstance().groupManager().getAllGroups());
    }

    private void initView() {
        mTitleBar = findViewById(R.id.etb_group_list);
        mListView = findViewById(R.id.lv_group_list);
        ll_create_new_group = findViewById(R.id.ll_create_new_group);

//        View headerView = View.inflate(this, R.layout.header_grouplist, null);
//        mListView.addHeaderView(headerView);
//
//        cl_create_new_group = headerView.findViewById(R.id.cl_create_new_group);
    }
    
    private void initListener() {
        mTitleBar.setOnBackPressListener(new EaseTitleBar.OnBackPressListener() {
            @Override
            public void onBackPress(View view) {
                onBackPressed();
            }
        });


        // 设置listview条目的点击监听
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                Toast.makeText(GroupListActivity.this, "打开群聊 position = " + position + " id = " + id, Toast.LENGTH_SHORT).show();

                Log.e(TAG, "onItemClick: 打开群聊 position = " + position );
//                if (position == 0){
//                    return;
//                }

                Intent intent = new Intent(GroupListActivity.this, GroupChatActivity.class);

                intent.putExtra(EaseConstant.EXTRA_CHAT_TYPE, EaseConstant.CHATTYPE_GROUP);

                EMGroup emGroup = EMClient.getInstance().groupManager().getAllGroups().get(position);

                intent.putExtra(EaseConstant.EXTRA_CONVERSATION_ID, emGroup.getGroupId());

                startActivity(intent);
            }
        });

        // 新建群组
        ll_create_new_group.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(GroupListActivity.this, CreateNewGroupActivity.class);

                startActivity(intent);
            }
        });

    }

    @Override
    protected void onResume() {
        super.onResume();

        refresh();
    }
}